import {createRouter, createWebHistory} from "vue-router";
import Home from '../views/main/Home.vue';
import Login from '../views/auth/Login.vue';
import Register from '../views/auth/Register.vue'
import BackGround from "../views/auth/BackGround.vue";
import StudentVideo from "../views/student/video/StudentVideo.vue";
import StudentPractice from "../views/student/practice/StudentPractice.vue";
import StudentChatAI from "../views/student/ai/StudentChatAI.vue";
import StudentSpace from "../views/student/space/StudentSpace.vue";
import Information from "../views/student/space/Information.vue";
import Inbox from "../views/student/space/Inbox.vue";
import Course from "../views/student/space/Course.vue";
import CollectionVideo from "../views/student/space/CollectionVideo.vue";
import CollectionPractice from "../views/student/space/CollectionPractice.vue";
import InsertUpdate from "../views/student/practice/InsertUpdate.vue";
import MyVideo from "../views/student/video/MyVideo.vue";
import VideoPlayPage from "../views/student/video/VideoPlayPage.vue";
import UploadVideo from "../views/student/video/UploadVideo.vue"
import NProgress from "nprogress";
import DoPractice from "../views/student/practice/DoPractice.vue";
import ToolBar from "../views/student/toolbar/ToolBar.vue";
import CourseSpace from "../views/course/CourseSpace.vue";
import HistoryPractice from "../views/student/space/HistoryPractice.vue";
import HistoryVideo from "../views/student/space/HistoryVideo.vue";
import CourseHome from "../views/course/CourseHome.vue";
import CourseClass from "../views/course/CourseClass.vue";
import CourseChapter from "../views/course/CourseChapter.vue";
import CourseTask from "../views/course/CourseTask.vue";
import CourseExams from "../views/course/CourseExams.vue";
import CourseAssignment from "../views/course/CourseAssignment.vue";
import CourseResource from "../views/course/CourseResource.vue";
import BackgroundLogin from "../background/auth/BackgroundLogin.vue";
import BackgroundRegister from "../background/auth/BackgroundRegister.vue";
import BackgroundHome from "../background/main/BackgroundHome.vue";
import ClassTable from "../views/course/CourseTable/ClassTable.vue";
import StudentTable from "../views/course/CourseTable/StudentTable.vue";
import ChapterManage from "../views/course/Mangement/ChapterManage.vue";
import AssignmentManage from "../views/course/Mangement/AssignmentManage.vue";
import ExamsManage from "../views/course/Mangement/ExamsManage.vue";
import TaskManage from "../views/course/Mangement/TaskManage.vue";
import ChapterOne from "../views/course/One/ChapterOne.vue";
import AssignmentOne from "../views/course/One/AssignmentOne.vue";
import ExamsOne from "../views/course/One/ExamsOne.vue";
import ClassPractice from "../views/course/Mangement/Task/ClassPractice.vue";
import Questionnaire from "../views/course/Mangement/Task/Questionnaire.vue";
import SignIn from "../views/course/Mangement/Task/SignIn.vue";
import TopicDiscuss from "../views/course/Mangement/Task/TopicDiscuss.vue";
import Comprehensive from "../views/student/space/information/ComprehensiveCircumstance.vue";
import PracticeCircumstance from "../views/student/space/information/PracticeCircumstance.vue";
import VideoCircumstance from "../views/student/space/information/VideoCircumstance.vue";
import CourseCircumstance from "../views/course/CourseCircumstance.vue";
import ClassPracticeOne from "../views/course/Mangement/Task/One/ClassPracticeOne.vue";
import QuestionnaireOne from "../views/course/Mangement/Task/One/QuestionnaireOne.vue";
import TopicDiscussOne from "../views/course/Mangement/Task/One/TopicDiscussOne.vue";
import SignInOne from "../views/course/Mangement/Task/One/SignInOne.vue";
import ParentsViews from "../views/main/ParentsViews.vue";
import InsertChapterTest from "../views/course/Mangement/InsertChapterTest.vue";
import InsertCourse from "../views/student/space/course/InsertCourse.vue";
import OJMain from '@/components/Main.vue'
import WebCode from '@/components/WebCode/WebCode.vue'
import Problem from '@/components/ProblemLib/Promblem.vue'
import UploadCode from '@/components/Manager/UploadCode.vue'
import UploadProblem from '@/components/Manager/UploadProblem.vue'
import UploadTestGroup from '@/components/Manager/UploadTestGroup.vue'
import Manager from '@/components/Manager/Manager.vue'
import ProblemDetail from '@/components/ProblemLib/ProblemDetail.vue'
import Commit from "@/components/ProblemLib/Commit.vue";
import CommitResult from "@/components/ProblemLib/CommitResult.vue";
import {checkToken} from "@/api/auth/loginApi.js";

// 进度条配置项
NProgress.configure({
    showSpinner: false
})

const router = createRouter({
    history:createWebHistory(),
    routes:[
        {
            path: '/',
            redirect: '/auth/login',
        },
        {
            path:'/home',
            component:Home,
            meta: {
                title: "首页",
                transition: "animate__fadeIn"
            }
        },
        {
            path:"/auth",
            component:BackGround,
            children:[{
                path:'login',
                component:Login,
            },
            {
                path:'register',
                component:Register,
            }]
        },
        {
            path:"/insertupdate/:id?",
            component:InsertUpdate,
        },
        {
            path:"/practice/:id",
            component:DoPractice,
        },
        {
            path: "/student",
            children: [{
                path: "video",
                component: StudentVideo,
            },
            {
                path: "playVideo/:id",
                component: VideoPlayPage,
            },
            {
                path: "practice",
                component: StudentPractice
            },
            {
                path: "ai",
                component: StudentChatAI
            },{
                path: "oj",
                component: OJMain,
                children: [{
                        path: 'WebCode',
                        component: WebCode
                    },
                    {
                        path: 'Problem',
                        component: Problem
                    },
                    {
                        path: 'UploadProblem',
                        component: UploadProblem
                    },
                    {
                        path: 'UploadCode',
                        component: UploadCode
                    }
                    ,
                    {
                        path: 'UploadTestGroup',
                        component: UploadTestGroup
                    }, {
                        path: 'Manager',
                        component: Manager
                    },
                    {
                        path: 'problem-detail/:id',
                        name: 'ProblemDetail',
                        component: ProblemDetail
                    }, {
                        path: 'Commit/:id',
                        component: Commit
                    }, {
                        path: 'CommitResult/:id',
                        name: 'CommitResult',
                        component: CommitResult,
                        props: true
                }]
            },
              {
                path: "space",
                component: StudentSpace,
                children:[{
                 path: "information",
                 component:Information,
                 children: [{
                     path: "comprehensive",
                     component: Comprehensive
                 },{
                     path: "practiceCircumstance",
                     component: PracticeCircumstance
                 },{
                     path: "videoCircumstance",
                     component: VideoCircumstance
                 }]
                },{
                     path: "inbox",
                     component:Inbox

                },{
                     path: "course",
                     component:Course
                },{
                    path:"collectionvideo",
                    component:CollectionVideo
                },{
                    path:"collectionpractice",
                    component:CollectionPractice
                },{
                     path: 'historypractice',
                     component: HistoryPractice
                },{
                    path: 'historyVideo',
                    component: HistoryVideo
                }]
            },{
                path: 'toolBar',
                component: ToolBar
            }]
        },
        {
            path: "/teacher",
            children: [{
                path: "uploadVideo",
                component: UploadVideo
            },{
                path: "myVideo",
                component: MyVideo
            }]
        },{
            path: "/course",
            component: CourseSpace,
            children: [{
                path: "home",
                component: CourseHome
             },{
                path: "courseCircumstance",
                component: CourseCircumstance
            },{
                path: "classmanage",
                component: CourseClass,
                children: [{
                    path: "classtable",
                    component: ClassTable
                },{
                    path: "studenttable",
                    component: StudentTable
                },]
            },{
                path: "chapter",
                component: CourseChapter
            },{
                path: "task",
                component: CourseTask
            },{
                path: "exams",
                component: CourseExams
            },{
                path: "assignment",
                component: CourseAssignment
            },{
                path: "resources",
                component: CourseResource
            }]
        },{
            path: "/chaptermanage",
            component: ChapterManage
        },{
            path:"/chaptertest/:id?",
            component: InsertChapterTest
        },{
            path: "/assignmentmanage",
            component: AssignmentManage
        },{
            path:"/examsmanage",
            component:ExamsManage
        },{
            path:"/taskmanage",
            component:TaskManage
        },{
            path: "/chapter/:id?",
            component:ChapterOne
        },{
            path: "/assignment/:id?",
            component:AssignmentOne
        },{
            path:"/exam/:id?",
            component:ExamsOne
        },{
            path:"/classpracticeone/:id?",
            component:ClassPracticeOne
        },{
            path:"/questionnaireone/:id?",
            component:QuestionnaireOne
        },{
            path:"/topicdiscussone/:id?",
            component:TopicDiscussOne
        },{
            path:"/signinone/:id?",
            component:SignInOne
        },{
            path:'/classpractice',
            component:ClassPractice
        },{
            path:"/questionnaire",
            component:Questionnaire
        },{
            path:"/signin",
            component:SignIn
        },{
            path:"/topicdiscuss",
            component:TopicDiscuss
        },{
            path: "/background",
            children: [{
                path: "login",
                component: BackgroundLogin,
            },{
                path: "register",
                component: BackgroundRegister,
            },{
                path: "home",
                component: BackgroundHome,
            }]
        },{
            path: "/parent",
            component: ParentsViews
        },{
            path: "/insertcourse",
            component: InsertCourse
        }
    ]
})


// 路由守卫
router.beforeEach((to, from, next) => {
    NProgress.start()
    const token = localStorage.getItem('token')
    //2个不用token的页面请求
    if (to.path === '/auth/login' || to.path === '/auth/register') {
        return next()
    }
    // 对于学校找好登录注册 同样需要放行
    if (to.path === '/background/login' || to.path === '/background/register') {
        return next();
    }
    // 如果token存在 先检查下
    if ( token ) {
        checkToken().then(res => {
            if ( res.data !== null ) {
                // token有效 直接放行
                return next();
            }else {
                // token无效 移除token 返回登陆页
                localStorage.removeItem('token')
                if ( to.path.indexOf('/background') !== -1) {
                    return next('/background/login')
                } else {
                    return next('/auth/login')
                }
            }
        })
    }else {
        //没有token的情况 直接返回登录页
        if (to.path.indexOf('/background') !== -1) {
            return next('/background/login')
        } else {
            return next('/auth/login')
        }
    }
    next()
})

router.afterEach(() => {
    NProgress.done() // 结束Progress
})

export default router;